package com.cloud.lan.integration.submail;

import com.alibaba.fastjson.JSON;
import com.cloud.lan.integration.submail.sms.entity.SmsSendModel;
import com.cloud.lan.integration.submail.sms.entity.SmsSendReq;
import com.cloud.lan.integration.submail.sms.entity.SmsSendRes;
import com.cloud.lan.integration.submail.sms.entity.SmsTemplateHandleRes;
import com.cloud.lan.integration.submail.sms.entity.SmsTemplateModel;
import com.cloud.lan.integration.submail.sms.entity.SmsTemplateQueryReq;
import com.cloud.lan.integration.submail.sms.entity.SmsTemplateQueryRes;
import com.cloud.lan.integration.submail.sms.service.SmsSendService;
import com.cloud.lan.integration.submail.sms.service.SmsTemplateService;
import com.cloud.lan.integration.submail.sms.service.impl.SmsSendServiceImpl;
import com.cloud.lan.integration.submail.sms.service.impl.SmsTemplateServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouquan on 2022/4/2.
 */
public class SmsTestDemo {

    public static void main(String[] args){
        //短信模板测试
        testSmsTemplate();

        //短信发送测试
        //testSmsSend("Ki4tG2");
    }

    private static void testSmsSend(String templateId){
        //模板方式批量发送短信
        SmsSendService smsSendService = new SmsSendServiceImpl();
        SmsSendReq smsSendReq = new SmsSendReq();
        smsSendReq.setTemplateId(templateId);
        List<SmsSendModel> modelList = new ArrayList<>();
        SmsSendModel ssm1 = new SmsSendModel();
        ssm1.setTo("13761231450");
        Map<String,String> vars1 = new HashMap<>();
        vars1.put("kuaidi","zq123");
        vars1.put("lanNum","40");
        ssm1.setVars(vars1);
        modelList.add(ssm1);
        smsSendReq.setModelList(modelList);
        List<SmsSendRes> resList = smsSendService.multixsend(smsSendReq);
        System.out.println("模板方式批量发送短信:"+JSON.toJSONString(resList));
    }

    private static void testSmsTemplate(){
        SmsTemplateService smsTemplateService = new SmsTemplateServiceImpl();
        //创建
        SmsTemplateModel model = new SmsTemplateModel();
        model.setSmsTitle("兰嘉丝汀短信");
        model.setSmsSignature("【兰嘉丝汀】");
        model.setSmsContent("提醒：有最新款式的护肤产品将在@var(lanDays)天后上架，总计数量@var(lanNum)件，先到先得请及时登录小程序购物");
        SmsTemplateHandleRes handleRes = smsTemplateService.create(model);
        System.out.println("创建:"+JSON.toJSONString(handleRes));
        Assert.isTrue(StringUtils.equals("success",handleRes.getStatus()),"创建失败");
        String templateId =  String.copyValueOf(handleRes.getTemplateId().toCharArray());
        Assert.isTrue(StringUtils.isNotBlank(templateId),"创建失败");

        //查询
        SmsTemplateQueryReq queryReq = new SmsTemplateQueryReq();
        SmsTemplateQueryRes queryRes = smsTemplateService.query(queryReq);
        System.out.println("创建查询:"+JSON.toJSONString(queryRes));
        long nums = queryRes.getTemplates().stream().filter(tmp->StringUtils.equals(templateId,tmp.getTemplateId()) && StringUtils.equals(model.getSmsSignature(),tmp.getSmsSignature())).count();
        Assert.isTrue(nums>0,"创建查询失败");

        //更新
        model.setTemplateId(templateId);
        model.setSmsSignature("【兰嘉丝汀小程序】");
        handleRes = smsTemplateService.update(model);
        System.out.println("更新:"+JSON.toJSONString(handleRes));
        Assert.isTrue(StringUtils.equals("success",handleRes.getStatus()),"更新失败");

        //查询
        queryReq.setTemplateId(templateId);
        queryRes = smsTemplateService.query(queryReq);
        System.out.println("更新查询:"+JSON.toJSONString(queryRes));
        nums = queryRes.getTemplates().stream().filter(tmp->StringUtils.equals(templateId,tmp.getTemplateId()) && StringUtils.equals(model.getSmsSignature(),tmp.getSmsSignature())).count();
        Assert.isTrue(nums>0,"更新查询失败");

        //删除
        handleRes = smsTemplateService.del(model);
        System.out.println("删除:"+JSON.toJSONString(handleRes));
        Assert.isTrue(StringUtils.equals("success",handleRes.getStatus()),"删除失败");

        //查询
        queryReq.setTemplateId(null);
        queryRes = smsTemplateService.query(queryReq);
        System.out.println("删除查询:"+JSON.toJSONString(queryRes));
        nums = queryRes.getTemplates().stream().filter(tmp->StringUtils.equals(templateId,tmp.getTemplateId()) && StringUtils.equals(model.getSmsSignature(),tmp.getSmsSignature())).count();
        Assert.isTrue(nums==0,"删除查询失败");
    }

}
